// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
	async indexAction(){
		//房间信息
		let rlist = await this.model('or_shop_room').where({s_id:this.shop_userinfo.s_id}).select();
		//分类信息
		let clist = await this.model('or_shop_roomcategory').where({s_id:this.shop_userinfo.s_id}).select();
		let data = {
			"rlist":rlist,
			"clist":clist,
		}
		return this.json(data);
	}
	
	
	//房间分类
	async categorylistAction(){
		if(this.isPost){
			let clist = await this.model('or_shop_roomcategory')
						.alias('sr')
						.field('sr.id,sr.name,sc.name as pname,sr.sort,sr.remarks')
						.join(think.config('db.prefix')+'or_shop_roomcategory sc on sr.pid=sc.id')
						.where({'sr.s_id':this.shop_userinfo.s_id})
						.select();
			let relist = {
				"draw": '',
				"data": clist,
				"breadcrumb":''
			}
			return this.json(relist);
		}
		return this.display();
	}
	
	//新增房间分类
	async addcategoryAction(){
		if(this.isPost){
			let data = this.post();
			data.o_id = this.shop_userinfo.o_id;
			data.o_code = this.shop_userinfo.o_code;
			data.s_id = this.shop_userinfo.s_id;
			//写入数据库
			let res = await this.model('or_shop_roomcategory').add(data);
			return res?this.success({info:"添加成功！"}):this.fail('添加失败！');
		}
		//取出分类列表
		let clist = await this.model('or_shop_roomcategory').field('id,name').where({s_id:this.shop_userinfo.s_id}).select();
		return this.json(clist);
	}
	
	//编辑房间分类
	async editcategoryAction(){
		if(this.isPost){
			let data = this.post();
			//自己不能是自己的上级
			let child = await this.getChild(data.id);
			if(data.id == data.pid || this.inArray(data.pid,child))
			{
				return this.fail('上级不能是自己或者自己的下级！');
			}
			let res = await this.model('or_shop_roomcategory').where({id:data.id}).update(data);
			return res?this.success({info:"编辑成功！"}):this.fail('编辑失败！');
		}
		let id = this.get('id');
		let cinfo = await this.model('or_shop_roomcategory').where({id:id}).find();
		let clist = await this.model('or_shop_roomcategory').field('id,name').where({s_id:this.shop_userinfo.s_id}).select();
		let data = {
			"clist":clist,
			"cinfo":cinfo,
		}
		return this.json(data);		
	}
	
	//删除分类
	async deletecategoryAction(){
		let id = this.post('id');
		//判断分类下是否有房间
		let is_room = await this.model('or_shop_room').where({c_id:id}).find();
		if (!think.isEmpty(is_room)) {
			return this.fail('请先删除或转移该分类下房间！');
		};
		let res = this.model('or_shop_roomcategory').where({id:id}).delete();
		return res?this.success({info:"删除成功！"}):this.fail('删除失败！');
	}

	//简单的取出子分类的函数
	async getChild(id){
		//取出子id
		let child_arr = [];
		let child = await this.model('or_shop_roomcategory').where({pid:id}).getField('id');
		if (!think.isEmpty(child)) {
			for (var i = child.length - 1; i >= 0; i--) {
				child_arr.push(child[i]);
				child_arr = child_arr.concat(await this.getChild(child[i]));
			};
		};
		return child_arr;	
	}

	//制作inarray函数
	inArray(id,array){
		if (!think.isEmpty(array)) {
			for (var i = 0; i < array.length; i++) {
				if (array[i] == id) {
					return true;
				};
			};
		};
		return false;
	}


	//新增房间
	async addroomAction(){
		if(this.isPost){
			let data = this.post();
			data.o_id = this.shop_userinfo.o_id;
			data.o_code = this.shop_userinfo.o_code;
			data.s_id = this.shop_userinfo.s_id;
			//写入数据库
			let res = await this.model('or_shop_room').add(data);
			return res?this.success({info:"添加成功！"}):this.fail('添加失败！');
		}
		//取出房间分类列表
		let clist = await this.model('or_shop_roomcategory').field('id,name').where({s_id:this.shop_userinfo.s_id}).select();
		return this.json(clist);
	}

	//房间列表
	async roomlistAction(){
		let rlist = await this.model('or_shop_room')
					.alias('sr')
					.join(think.config('db.prefix')+'or_shop_roomcategory sc on sr.c_id = sc.id')
					.field('sr.id,sr.name,sr.status,sc.name as cname')
					.where({'sr.s_id':this.shop_userinfo.s_id})
					.select();
		let relist = {
				"draw": '',
				"data": rlist,
				"breadcrumb":''
			}
		return this.json(relist);
	}

	//编辑房间
	async editroomAction(){
		if (this.isPost) {
			let data = this.post();
			let res = await this.model('or_shop_room').where({id:data.id}).update(data);
			return res?this.success({info:"编辑成功！"}):this.fail('编辑失败！');
		};
		let id = this.get('id');
		let rinfo = await this.model('or_shop_room').where({id:id}).find();
		let clist = await this.model('or_shop_roomcategory').select();
		let data = {
			"rinfo":rinfo,
			"clist":clist
		}
		return this.json(data);
	}

	//删除房间
	async deleteroomAction(){
		let id = this.post('id');
		let res = this.model('or_shop_room').where({id:id}).delete();
		return res?this.success({info:"删除成功！"}):this.fail('删除失败！');
	}
}